package de.duester.vectordrawing;

import java.util.ArrayList;
import java.util.List;

import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.PointF;
import android.graphics.drawable.Drawable;

/**
 * @author Maxim Duester
 */
public class Scene implements IPaintable {
	private boolean visible = true;
	private List<Layer> layers = new ArrayList<Layer>();
	private int backgroundColor = Color.WHITE;
	private Drawable backgroundDrawable = null;

	/**
	 * 
	 */
	public Scene() {
		// TODO Scene()
	}

	/**
	 * @param layer
	 */
	public void addLayer(Layer layer) {
		if (!layers.contains(layer))
			layers.add(layer);
	}

	/**
	 * @param layer
	 * @param position
	 */
	public void insertLayer(Layer layer, int position) {
		if (!layers.contains(layer))
			layers.add(position, layer);
	}

	/**
	 * @param layer
	 */
	public void removeLayer(Layer layer) {
		layers.remove(layer);
	}

	/**
	 * @param position
	 * @throws IndexOutOfBoundsException
	 */
	public void removeLayer(int position) throws IndexOutOfBoundsException {
		layers.remove(position);
	}

	/**
	 * @param name
	 */
	public void removeLayer(String name) {
		removeLayer(getLayer(name));
	}

	/**
	 * @param position
	 * @return
	 * @throws IndexOutOfBoundsException
	 */
	public Layer getLayer(int position) throws IndexOutOfBoundsException {
		return layers.get(position);
	}

	/**
	 * @param name
	 * @return
	 */
	public Layer getLayer(String name) {
		for (Layer l : layers)
			if (l.getName().equals(name))
				return l;
		return null;
	}

	/**
	 * @param layer
	 * @return
	 */
	public int getLayerPosition(Layer layer) {
		return layers.indexOf(layer);
	}

	/**
	 * @param layer
	 * @return
	 */
	public boolean contains(Layer layer) {
		return layers.contains(layer);
	}

	/**
	 * @return
	 */
	public int size() {
		return layers.size();
	}

	/**
	 * 
	 */
	public void clear() {
		layers.clear();
	}

	/**
	 * @param backgroundColor
	 */
	public void setBackgroundColor(int backgroundColor) {
		this.backgroundColor = backgroundColor;
	}

	/**
	 * @return
	 */
	public int getBackgroundColor() {
		return backgroundColor;
	}

	/**
	 * @param backgroundDrawable
	 */
	public void setBackgroundDrawable(Drawable backgroundDrawable) {
		this.backgroundDrawable = backgroundDrawable;
	}

	/**
	 * @return
	 */
	public Drawable getBackgroundDrawable() {
		return backgroundDrawable;
	}

	/* (non-Javadoc)
	 * @see de.duester.vectordrawing.IModifiable#move(android.graphics.PointF)
	 */
	public void move(PointF p) {
		for (Layer layer : layers)
			layer.move(p);
	}

	/* (non-Javadoc)
	 * @see de.duester.vectordrawing.IModifiable#moveRelative(android.graphics.PointF)
	 */
	public void moveRelative(PointF p) {
		for (Layer layer : layers)
			layer.moveRelative(p);
	}

	/* (non-Javadoc)
	 * @see de.duester.vectordrawing.IModifiable#rotate(int, android.graphics.PointF)
	 */
	public void rotate(int angle, PointF p) {
		for (Layer layer : layers)
			layer.rotate(angle, p);
	}

	/* (non-Javadoc)
	 * @see de.duester.vectordrawing.IModifiable#rotateAndMove(int, android.graphics.PointF)
	 */
	public void rotateAndMove(int angle, PointF p) {
		for (Layer layer : layers)
			layer.rotateAndMove(angle, p);
	}

	/* (non-Javadoc)
	 * @see de.duester.vectordrawing.IModifiable#scale(double, android.graphics.PointF)
	 */
	public void scale(double factor, PointF p) {
		for (Layer layer : layers)
			layer.scale(factor, p);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see de.duester.vectordrawing.IPaintable#setVisible(boolean)
	 */
	@Override
	public void setVisible(boolean visible) {
		this.visible = visible;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see de.duester.vectordrawing.IPaintable#isVisible()
	 */
	@Override
	public boolean isVisible() {
		return visible;
	}

	/* (non-Javadoc)
	 * @see de.duester.vectordrawing.IPaintable#paint(android.graphics.Canvas)
	 */
	public void paint(Canvas canvas) {
		if (visible)
			for (Layer layer : layers)
				if (layer.isVisible())
					layer.paint(canvas);
	}
}
